import { defineConfig } from 'vite'
import uni from '@dcloudio/vite-plugin-uni'
import path from 'path'
import AutoImport from 'unplugin-auto-import/vite'
import h5ProdEffectPlugin from 'uni-vite-plugin-h5-prod-effect'

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const isProduction = mode === 'production'
  //  环境变量是 tenxun， 则打包到 mall/uni/ （nginx的访问地址前缀）
  return {
    base: (() => {
      switch (mode) {
        case 'tenxun':
          return '/car/uni/'
        case 'production':
        case 'testing':
        case 'development':
        default:
          return '/'
      }
    })(),
    plugins: [
      uni(),
      AutoImport({
        imports: [
          'vue',
          'uni-app'
        ],
        dirs: [
          'src/utils',
          'src/wxs/**'
        ],
        dts: 'src/auto-imports.d.ts',
        eslintrc: {
          enabled: true
        }
      }),
      // 对h5 production环境打包时的特殊处理，否则uni-crazy-router在这个环境会异常
      h5ProdEffectPlugin()
    ],
    server: {
      host: true,
      port: 80,
      open: true
    },
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src')
      }
    },
    build: isProduction ? {
      outDir: 'dist',
      assetsDir: 'assets',
      emptyOutDir: true,
      rollupOptions: {
        output: {
          entryFileNames: 'assets/[name].[hash].js',
          chunkFileNames: 'assets/[name].[hash].js',
          assetFileNames: 'assets/[name].[hash].[ext]'
        }
      }
    } : undefined
  }
})
